<template>
  <jy-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
    <!-- <div class="note-border"></div> -->
    <!-- {{ this.form.projectId }} -->
    <el-card shadow="never" v-loading="buttonLoading">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-row :gutter="6">
          <el-col :span="12" v-for="(item, index) in titleList" :key="index">
            <el-form-item :label="item.name">
              <el-input v-model="form[item.code]" disabled :placeholder="`请输入${item.name}`" maxlength="100">
                <template slot="append" v-if="item.type == 'money'">元</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <div class="assessing-officer">审批人员</div>
        <el-row :gutter="20">
          <el-col :span="20">
            <assessing-officer ref="assessingOfficerRef" :examinerData="examinerList"></assessing-officer>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" :loading="buttonLoading" @click="submitForm">发起审批
      </el-button>
      <el-button @click="cancel">关 闭</el-button>
    </div>
    <address-Book ref="addressBookRef" @callBack="addUser"></address-Book>
  </jy-dialog>
</template>
<script>
import { startHTZDQDSPProcess, getHTZDQDSPPerson, startHTZDQDBGSPProcess, getHTZDQDBGSPPerson } from "@/api/workflow/business";
import { processPersonInfo, contractListInfo } from '@/components/AssessingOfficer/data';

export default {
  name: 'postpone',
  data () {
    return {
      // 弹出层标题
      title: "发起审批",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      titleList: [
        //   { code: 'contractName', name: '合同名称' },
      ],
      // 表单校验
      rules: {
      },
      buttonLoading: false,
      examinerList: []
    };
  },
  created () {
  },
  mounted () {
  },
  methods: {
    /** 新增按钮操作 */
    init (info, isGroupPro) {
      this.reset();
      this.open = true;
      this.title = `${info.projectName}发起审批`;
      // this.title = `发起审批`;
      this.isGroupPro = isGroupPro;
      for (const key in info) {
        if (Object.hasOwnProperty.call(info, key)) {
          const element = info[key];
          this.form[key] = element;
        }
      }
      this.getPerson();
    },
    // 获取预设审批人
    getPerson () {
      const request = this.form.type == 'HTZDQDSP' ? getHTZDQDSPPerson : getHTZDQDBGSPPerson;
      request(this.form.zdId).then((response) => {
        const data = response.data;
        // 取出对应名称
        let info = contractListInfo;
        // 拼接人员列表
        let list = [];
        for (const key in info) {
          if (Object.hasOwnProperty.call(info, key)) {
            const item = (data && data[key]) ? data[key] : { userId: undefined, nickName: undefined };
            const taskName = processPersonInfo[key];
            const taskType = key;
            list.push({
              taskName, taskType, ...item
            });
          }
        }
        this.examinerList = list;
      }).catch((error) => {
        this.examinerList = [];
      });
    },

    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          const info = this.$refs.assessingOfficerRef.returnInfo();
          if (!info) {
            return;
          }
          this.buttonLoading = true;
          if (this.form.projectId != undefined) {
            const request = this.form.type == 'HTZDQDSP' ? startHTZDQDSPProcess : startHTZDQDBGSPProcess;
            request({ ...this.form, ...info }).then((response) => {
              this.$modal.msgSuccess("发起成功");
              this.cancel();
              this.$emit('callBack');
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    // 选择人员
    selectUser (code) {
      this.$refs.addressBookRef.init(code);
    },
    // 添加人员
    addUser (info) {
      this.form[info.code] = info.userId;
      this.form[info.code + 'Name'] = info.nickName;
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
      this.$refs.assessingOfficerRef.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: undefined,
        projectId: undefined,//
        projectName: undefined,//
        type: undefined,
        jianli: undefined, // 监理
        jianliName: undefined, // 监理
        shenji: undefined, // 审计
        shenjiName: undefined, // 审计
        chushen: undefined, // 初审
        chushenName: undefined, // 初审
        zhongshen: undefined, // 终审
        zhongshenName: undefined, // 终审
        jianshedanwei: undefined, // 建设单位
        jianshedanweiName: undefined, // 建设单位
      };
      this.resetForm("form");
    },
  },
};
</script>
<style lang="scss" scoped>
.note-border {
  text-align: center;
  padding-bottom: 20px;
}

.form-item-border {
  display: flex;
  align-items: center;
  justify-content: space-around;
  padding-bottom: 100px;
}

.assessing-officer {
  font-size: 16px;
  font-weight: 600;
  padding-bottom: 10px;
}
</style>
